import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:library_base/utils/sp_util.dart';

extension DoubleExt on double? {
  bool isNotZeroAndNull() {
    return this != null && this != 0;
  }

  String numAsUnitStr() {
    return SPUtil.isJin? (this!*2).toString():this!.toString();
  }

  double numAsUnit() {
    return SPUtil.isJin? this!*2:this!;
  }

  ///把服务器金额转换成本地的
  double? remoteAsLocalUnit(String sourceUnit) {
    if(this == null){
      return this;
    }else{
      if(sourceUnit == ServiceUnit.JIN.name){
        if(SPUtil.isJin){
          return this!;
        }else{
          return NumUtil.multiply(this!, 2);
        }
      }else if(sourceUnit == ServiceUnit.KG.name){
        if(SPUtil.isJin){
          return NumUtil.divide(this!, 2);
        }else{
          return this!;
        }
      }else{
        if(SPUtil.isJin){
          return NumUtil.divide(this!, 2);
        }else{
          return this!;
        }
      }
    }
  }

  double numAsUnitSubmit() {
    return SPUtil.isJin? NumUtil.divide(this!, 2):this!;
  }

  String numAsUnitSubmitStr(bool unitIsJin) {
    if(unitIsJin){
      return this!.toString();
    }else{
      return NumUtil.divide(this!, 2).toString();
    }
  }

  ///服务器单位转换成本地单位
  double localUnitToRemoteUnit(String sourceUnit){
    if(sourceUnit == ServiceUnit.JIN.name){
      if(SPUtil.isJin){
        return this!;
      }else{
        return NumUtil.divide(this!, 2);
      }
    }else if(sourceUnit == ServiceUnit.KG.name){
      if(SPUtil.isJin){
        return NumUtil.multiply(this!, 2);
      }else{
        return this!;
      }
    }else{
      if(SPUtil.isJin){
        return NumUtil.multiply(this!, 2);
      }else{
        return this!;
      }
    }
  }

  ///服务器重量单位转换成本地重量单位 数字
  double remoteUnitTransitionLocalUnit(String sourceUnit){
    if(sourceUnit == ServiceUnit.JIN.name){
      if(SPUtil.isJin){
        return this!;
      }else{
        return NumUtil.divide(this!, 2);
      }
    }else if(sourceUnit == ServiceUnit.KG.name){
      if(SPUtil.isJin){
        return NumUtil.multiply(this!, 2);
      }else{
        return this!;
      }
    }else if(sourceUnit == ServiceUnit.FEN.name){
      return numAsUnit();
    }
    return numAsUnit();
  }

  ///服务器重量单位转换成本地重量单位 字符串
  String remoteUnitTransitionLocalUnitStr(String sourceUnit){
    return remoteUnitTransitionLocalUnit(sourceUnit).toString();
  }

}
